/**
 * Description: 设置 APP
 *
 * @author      Eden
 * @datetime    2016/11/26 22:52
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

(function () {


    // App 描述
    var appDescription = UE.getEditor('appDescription', {
        'autoClearinitialContent' : false,
        'toolbars' : [['bold', 'italic', 'underline', 'forecolor', 'backcolor', '|',
            'justifyleft', 'justifycenter', 'justifyright', '|', 'insertorderedlist', 'insertunorderedlist',
            'link', 'removeformat', '|', 'paragraph', 'fontsize']],
        'elementPathEnabled' : false,
        'initialFrameHeight': 200,
        'focus' : false,
        'maximumWords' : 9999,
        'zIndex': 999
    });

    var appPrivicy = UE.getEditor('appPrivicy', {
        'autoClearinitialContent' : false,
        'toolbars' : [['bold', 'italic', 'underline', 'forecolor', 'backcolor', '|',
            'justifyleft', 'justifycenter', 'justifyright', '|', 'insertorderedlist', 'insertunorderedlist',
            'link', 'removeformat', '|', 'paragraph', 'fontsize']],
        'elementPathEnabled' : false,
        'initialFrameHeight': 200,
        'focus' : false,
        'maximumWords' : 9999,
        'zIndex': 999
    });

    var api = {
        "getConfigUrl": "/system/app/getConfig",
        "saveConfigUrl": "/system/app/saveConfig"
    };

    sm.controller('admin-system-app-controller', ['$scope', '$http', 'Modal', function ($scope, $http, Modal) {

        $scope.general = {
            appName: "",
            logo: "",
            slideNum: 0,
            slideTimer: 0,
            slideType: ''
        };

        $scope.download = {
            ios: {
                version: "",
                address: ""
            },
            android: {
                version: "",
                address: ""
            }
        };


        $scope.description = {
            content: ""
        };
        $scope.privicy = {
            content: ""
        };
        $scope.adv = {
            imageUrl: ""
        };
        $scope.showAdvUrl = "";
        $scope.showLogoUrl = '';

        /* BOOTSTRAP SLIDER */
        var s = $('.slider').slider({
            formatter: function(value) {
                return value + '秒';
            }
        }).on('change', function (res) {
            $scope.general.slideTimer = s.slider('getValue');
        });
        $scope.general.slideTimer = s.slider('getValue');


        $http.get(api.getConfigUrl).success(function (res) {

            if (res.state) {
                $scope.showLogoUrl = '';
                if (res.data.general) {
                    $scope.general = angular.extend($scope.general, res.data.general);
                }
                if (res.data.download) {
                    $scope.download = res.data.download;
                }
                if (res.data.description) {
                    appDescription.ready(function () {
                        appDescription.setContent(res.data.description.content);
                    });
                }
                if (res.data.privicy) {
                    appPrivicy.ready(function () {
                        appPrivicy.setContent(res.data.privicy.content);
                    });
                }

                if (res.data.show) {
                    $scope.showAdvUrl = res.data.show.advUrl;
                    $scope.showLogoUrl = res.data.show.logoUrl;
                }
            }
        });

        $scope.saveSetting = function (type) {

            if (type !== 'general' && type !== 'download' && type !== 'adv' && type !== 'description' && type !== 'privicy') {
                return false;
            }

            if (type == 'description') {
                appDescription.ready(function () {
                    $scope.description['content'] = appDescription.getContent();
                });
            }

            if (type == 'privicy') {
                appPrivicy.ready(function () {
                    $scope.privicy['content'] = appPrivicy.getContent();
                });
            }

            // 保存
            $http.post(api.saveConfigUrl + "?key=" + type, $scope[type]).success(function (res) {
                if (res.state) {
                    Modal.success('设置成功');
                } else {
                    var error = res.error || '设置失败';
                    Modal.error(error);
                }
            });
        };


        // ===========
        // 初始化Web Uploader
        uploader = WebUploader.create({
            auto: true,
            server: '/system/app/upload?type=logo',
            pick: '#logoBtn',
            swf: '/assets/library/webuploader/Uploader.swf',
            duplicate: true,
            accept: {
                title: 'Images',
                extensions: 'gif,jpg,jpeg,png'
            }
        });

        // 成功
        uploader.on( 'uploadSuccess', function(file, res) {
            if (res.state) {
                $scope.general.logo = res.data.key;
                $scope.showLogoUrl = res.data.fullPath;
            } else {
                var error = res.error ? res.error : '上传失败';
                Modal.error(error);
            }
        });

        uploader.on( 'uploadComplete', function( file ) {
            $scope.$apply();
        });

        upAdv = WebUploader.create({
            auto: true,
            server: '/system/app/upload?type=adv',
            pick: '#advBtn',
            swf: '/assets/library/webuploader/Uploader.swf',
            duplicate: true,
            accept: {
                title: 'Images',
                extensions: 'gif,jpg,jpeg,png'
            }
        });

        // 上传之前
        upAdv.on( 'uploadSuccess', function(file, res) {
            if (res.state) {
                $scope.showAdvUrl = res.data.fullPath;
                $scope.adv.imageUrl = res.data.key;
            } else {
                var error = res.error ? res.error : '上传失败';
                Modal.error(error);
            }
        });

        upAdv.on( 'uploadComplete', function( file ) {
            $scope.$apply();
        });
    }]);

})();